package com.buka.service.impl;

import com.buka.http.AppInfo;
import com.buka.mapper.ArrangeMapper;
import com.buka.mapper.CinemaHouseMapper;
import com.buka.mapper.CinemaMapper;
import com.buka.mapper.MovieMapper;
import com.buka.pojo.Arrange;
import com.buka.pojo.Cinema;
import com.buka.pojo.CinemaHouse;
import com.buka.pojo.Movie;
import com.buka.service.CinemaService;
import com.buka.utils.ResponseJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * CinemaServiceImpl 实现类
 */
@Service
public class CinemaServiceImpl implements CinemaService {

    @Autowired
    private CinemaMapper cinemaMapper;

    @Autowired
    private CinemaHouseMapper cinemaHouseMapper;

    @Autowired
    private MovieMapper movieMapper;

    @Autowired
    private ArrangeMapper arrangeMapper;

    /**
     * 影院端登录
     */
    public ResponseJson login(String username, String password, HttpSession httpSession) {
        Cinema cinema = cinemaMapper.getCinemaByUsername(username);
        if (cinema != null) {
            if (cinema.getCinemaState() == 0) {
                return ResponseJson.getError("已被封号，请联系客服");
            } else if (cinema.getCinemaPassword().equals(password)) {
                httpSession.setAttribute(AppInfo.SESSION_CINEMA, cinema);
                return ResponseJson.getOK();
            } else {
                return ResponseJson.getError("密码错误");
            }
        }
        return ResponseJson.getError("用户不存在");
    }

    /**
     * 添加影厅
     */
    public ResponseJson addHouse(CinemaHouse cinemaHouse, HttpSession httpSession) {
        Cinema cinema = getPojoBySession(httpSession);
        cinemaHouse.setChCinemaId(cinema.getCinemaId());
        int i = cinemaHouseMapper.add(cinemaHouse);
        return i > 0 ? ResponseJson.getOK() : ResponseJson.getError();
    }

    /**
     * 影厅列表
     */
    public ResponseJson getHouseList(HttpSession httpSession) {
        Cinema cinema = getPojoBySession(httpSession);
        List<CinemaHouse> cinemaHouseList = cinemaHouseMapper.getList(cinema.getCinemaId());
        return ResponseJson.getOK(cinemaHouseList);
    }

    /**
     * 设置排片
     */
    public ResponseJson addArrange(int movieId, int houseId, Date startTime, int money, HttpSession session) {
        Cinema cinema = getPojoBySession(session);
        Movie movie = movieMapper.getMovieById(movieId);
        long endTime = startTime.getTime() + (long) movie.getMovieTime() * 60 * 1000;
        Arrange arrange = new Arrange(cinema.getCinemaId(), houseId, movieId, startTime, new Date(endTime), money);
        int i = arrangeMapper.add(arrange);
        return i > 0 ? ResponseJson.getOK() : ResponseJson.getError();
    }

    /**
     * 电影列表(能放映)
     */
    public ResponseJson getMovieList() {
        List<Movie> movieList = movieMapper.getMovieListByState(1);
        return ResponseJson.getOK(movieList);
    }


    private Cinema getPojoBySession(HttpSession httpSession) {
        return (Cinema) httpSession.getAttribute(AppInfo.SESSION_CINEMA);
    }
}